var packageName = "com.tencent.mm"; // 视频App的包名

// 打开视频App
launch(packageName);
toast('打开微信')

// App加载完成
waitForPackage(packageName);
toast('加载完成')
sleep(6000)


//打开发现
toast('打开发现')
id("icon_tv").className("android.widget.TextView").text("发现").findOne().parent().parent().click()
sleep(6000)

//点击视频号

var title =className('android.widget.TextView').text('视频号').findOne(2000)
if(title){
    console.log('找到了');
    click(title.bounds().centerX(),title.bounds().centerY())
}else{
    console.log('没找到');
}
toast('打开视频号')
sleep(10000)

const searchSPH = className("android.widget.ImageView").desc("搜索").findOne(2000)
click(searchSPH.bounds().centerX(), searchSPH.bounds().centerY())
sleep(random(1000,3000))

const arrSPH = ['阿北说流量','久久五2632']

arrSPH.forEach(e=>{
    // 输入搜索内容
  const searchEditSPH = className('android.widget.EditText').depth('14').drawingOrder('5').findOne(2000)
  searchEditSPH.setText(e)
  sleep(random(1000,3000))
  // 点击搜索按钮
  const searchBtnSPH = className('android.widget.TextView').depth('14').drawingOrder('7').text('搜索').findOne(2000)
  searchBtnSPH.click()
  sleep(random(3000,4000))
  const usersSPH = className('android.view.View').depth('26').drawingOrder('0').text('账号,按钮,2之2').findOne(2000)
  if(usersSPH){
    usersSPH.click()
  }else{
    const usersSPH1 = className('android.view.View').depth('26').drawingOrder('0').text('已选定,账号,按钮,2之2').findOne(2000)
    usersSPH1.click()
  }
  sleep(random(1000,3000))
  const userItemSPH = className('android.view.View').depth('25').drawingOrder('0').findOne(2000)
  click(userItemSPH.bounds().centerX(),userItemSPH.bounds().centerY())
  sleep(random(5000,6000))
  const videoItemSPH = id('fy9').findOne(2000)
  click(videoItemSPH.bounds().centerX(),videoItemSPH.bounds().centerY())
  sleep(random(1000,3000))
  isReviewAndThreeDay()
})


// 生成随机数函数
function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 判断是否有评论和是否在三天内
function isReviewAndThreeDay(){
  const ranSPH = id('ran').depth('21').findOne(2000)
    const reviewSPH = className('android.widget.LinearLayout').depth('21').drawingOrder('5').findOne(2000)
    if(reviewSPH){
      if(reviewSPH.child(1).text()!='评论'){
        reviewSPH.click()
      }else{
        swipe(
          device.width / 2,
          device.height * 0.8,
          device.width / 2,
          device.height * 0.2,
          random(500,1000)
        )
        sleep(random(500,1000))
        isReviewAndThreeDay()
      }
    }else{
      className('android.widget.LinearLayout').depth('19').drawingOrder('5').findOne(2000).click()
    }
    findReviewSPH()
    sleep(random(1000,3000))
}


// 查找评论
function findReviewSPH() {
  let reviewItemSPH = className('android.widget.FrameLayout').depth('7').find()
  reviewItemSPH = reviewItemSPH.filter(e=>e.childCount()==1&&(e.child(0).childCount()==1||e.child(0).childCount()==2)&&e.child(0).child(0).childCount()>3)
  console.log(reviewItemSPH.length);
  reviewItemSPH.forEach(e=>{
    let strTextSPH = e.childCount()>1?e.child(1).child(0).child(2).child(0):e.child(0).child(0).child(2).child(0)
    console.log(strTextSPH.text());
      if(storages.create('SPHReview').contains(strTextSPH.parent().parent().child(1).child(0).text())){
          // 已回复过
          console.log('已回复过' + strTextSPH.parent().parent().child(1).child(0).text());
      }else{
          // 截流
          storages.create('SPHReview').put(strTextSPH.parent().parent().child(1).child(0).text(),strTextSPH.parent().parent().child(1).child(0).text())
          jieLiuSPH(e)
      }
      sleep(random(1000,3000))
    })
  const bottomReviewSPH = id('bdv').className('android.view.View').depth('8').drawingOrder('1').findOne(2000)
  console.log(bottomReviewSPH);
  if(bottomReviewSPH&&bottomReviewSPH.bounds().right>0){
    toastLog('到底了')
    back()
    sleep(random(500,1000))
    swipe(
      device.width / 2,
      device.height * 0.8,
      device.width / 2,
      device.height * 0.2,
      random(500,1000)
    )
    sleep(random(500,1000))
    const bottomReviewSPH2 = id('bdv').className('android.view.View').depth('16').drawingOrder('1').findOne(2000)
    if(bottomReviewSPH2){
      toastLog('没有视频了')
      back()
      sleep(random(500,1000))
      back()
      sleep(random(500,1000))
      back()
      sleep(random(500,1000))
      return
    }
    const ranSPH = id('ran').depth('21').findOne(2000)
    // if(!isThreeDay(ranSPH.text())){
    //   toastLog('该视频不在三天内')
    //   back()
    //   sleep(random(500,1000))
    //   back()
    //   sleep(random(500,1000))
    //   back()
    //   sleep(random(500,1000))
    //   return
    // }
    const reviewSPH = className('android.widget.LinearLayout').depth('21').drawingOrder('5').findOne(2000)
    if(reviewSPH){
      reviewSPH.click()
    }else{
      className('android.widget.LinearLayout').depth('19').drawingOrder('5').findOne(2000).click()
    }
    sleep(random(1000,3000))
    findReviewSPH()
  }else{
    swipe(
      device.width / 2,
      device.height * 0.8,
      device.width / 2,
      device.height * 0.2,
      random(500,1000)
    )
    sleep(random(500,1000))
    findReviewSPH()
  }
}
// 截流函数
function jieLiuSPH(e){
  e.child(0).child(0).child(0).click()
  sleep(random(3000,5000))
  const siXinBtnSPH = className('android.widget.TextView').depth('12').drawingOrder('4').text('私信').findOne(1000)
  const siXinBtnSPH2 = className('android.widget.TextView').depth('17').drawingOrder('1').text('私信').findOne(1000)
  const siXinBtnSPH3 = className('android.widget.TextView').depth('16').drawingOrder('1').text('视频号').findOne(1000)
  // if(siXinBtnSPH){
  //   sleep(random(1000,1500))
  //   console.log(id('g5g').findOne(2000).text());
  //   console.log(id('g5f').findOne(2000).text());
  //   // jieLiuSXSPH()
  //   return
  // }else 
  if(siXinBtnSPH2){
    sleep(random(1000,1500))
    console.log(id('fzn').className('android.widget.TextView').depth('18').drawingOrder('1').findOne().text());
    console.log(id('ov9').className('android.widget.TextView').depth('18').drawingOrder('1').findOne().text());
    back()
    // jieLiuSXSPH()
  }else if(siXinBtnSPH3){
    click(siXinBtnSPH3.bounds().centerX(),siXinBtnSPH3.bounds().centerY())
    sleep(random(1000,1500))
    // const siXinBtnPage2SPH = className('android.widget.TextView').depth('17').drawingOrder('1').text('私信').findOne(1000)
    // click(siXinBtnPage2SPH.bounds().centerX(),siXinBtnPage2SPH.bounds().centerY())
    sleep(random(1000,2000))
    console.log(id('fzn').className('android.widget.TextView').depth('18').drawingOrder('1').findOne().text());
    console.log(id('ov9').className('android.widget.TextView').depth('18').drawingOrder('1').findOne().text());
    back()
    sleep(random(1000,2000))
    back()
    // jieLiuSXSPH()
  }else{
    back()
  }
}

// 截流私信函数
function jieLiuSXSPH(){
  const jiuLiuSXInputSPH = className('android.widget.EditText').depth('21').drawingOrder('1').findOne(2000)
  jiuLiuSXInputSPH.setText('你好')
  sleep(random(1000,2000))
  className('android.widget.Button').text('发送').findOne(2000).click()
  back()
  sleep(random(1000,3000))
  back()
}

// 判断是不是三天前
function isThreeDay(date){
  if(date.includes('前')){
    if(date.includes('天')){
      if(date.split('天')[0] <= 3){
        return true
      }else{
        return false
      }
    }
    if(date.includes('小时')){
      return true
    }
  }else if(date.includes('刚')){
    return true
  }else{
    return false
  }
  
}